home *** CD-ROM | disk | FTP | other *** search
- /*
- PFSDefragTry
- $Id: PFSDefragTry.e 1.5 1999/10/08 18:15:35 helios Exp $
- */
-
- OPT OSVERSION=37
- OPT REG=5
-
- MODULE 'dos/dos'
- MODULE 'exec/memory'
-
- CONST MAXLINELEN=1000, BUFLEN=65536
- CONST TEMPFILELEN=100
-
- ENUM OK,ER_BADARGS,ER_DISKVALID,ER_DELETE,ER_COPYING,ER_EXAMINE,ER_PROT,ER_DATE,ER_COMMENT,ER_NOLOCK,BREAK
- ENUM ARG_DEVICE,ARG_TEMPDIR,ARG_BUFLEN,NUMARGS
-
- PROC delfile(file)
- DEF i
-
- i := DeleteFile(file)
- IF i = FALSE
- i := SetProtection(file, 0)
- IF i= TRUE
- i := DeleteFile(file)
- IF i = FALSE THEN PrintF('Deleting file "\s" failed!', file)
- ELSE
- PrintF('Removing delete protection from file "\s" failed!\n', file)
- ENDIF
- ENDIF
- ENDPROC i
-
- PROC copyfile(from=NIL,to=NIL,buffer=NIL,buflen=0)
- DEF ok=FALSE, ready=FALSE, readlen, writelen, fh_from=NIL, fh_to=NIL
-
- IF fh_from:=Open(from,MODE_OLDFILE)
- IF fh_to:=Open(to,MODE_NEWFILE)
- IF buffer
- IF buflen
- REPEAT
- readlen:=Read(fh_from, buffer, buflen)
- IF readlen<buflen THEN ready:=TRUE
- IF readlen<>TRUE
- IF readlen>0
- writelen:=Write(fh_to, buffer, readlen)
- ELSE
- writelen:=0
- ENDIF
- IF writelen<>readlen
- ready:=TRUE
- ELSE
- IF ready THEN ok:=TRUE
- ENDIF
- ENDIF
- UNTIL ready=TRUE
- ENDIF
- ENDIF
- Close(fh_to)
- ENDIF
- Close(fh_from)
- ENDIF
- ENDPROC ok
-
- PROC main() HANDLE
- DEF fh_fragfiles=NIL,i,s[MAXLINELEN]:STRING,file[MAXLINELEN]:STRING
- DEF rdargs=NIL, templ, args[NUMARGS]:LIST, buffer=NIL, buflen=BUFLEN
- DEF tempfile[TEMPFILELEN]:STRING
- DEF lock=NIL:LONG
- DEF fib=NIL:PTR TO fileinfoblock
- DEF examineok:LONG
-
- args:=[NIL,NIL,NIL]
-
- templ:='DEVICE/A,TEMPDIR,BUFLEN/N'; rdargs:=ReadArgs(templ,args,NIL)
- IF rdargs=NIL THEN Raise(ER_BADARGS)
-
- IF args[ARG_BUFLEN]
- buflen:=Long(args[ARG_BUFLEN])
- IF buflen<4096 THEN buflen:=4096
- ENDIF
-
- PrintF('Examing \s with DiskValid... ',args[ARG_DEVICE])
- Flush(stdout)
-
- StringF(s,'diskvalid \s analyse >RAM:fragfiles',args[ARG_DEVICE])
- i:=SystemTagList(s, NIL)
- IF i<>0
- PrintF('failed!\n')
- Raise(ER_DISKVALID)
- ENDIF
- PrintF('ok.\n')
-
- IF fh_fragfiles:=Open('RAM:fragfiles',MODE_OLDFILE)
-
- PrintF('Allocating io buffer... ')
- Flush(stdout)
- buffer:=AllocVec(buflen,MEMF_PUBLIC)
- IF buffer=NIL
- PrintF('failed!\n')
- Raise("MEM")
- ENDIF
- PrintF('ok.\n')
-
- IF args[ARG_TEMPDIR]
- tempfile:=args[ARG_TEMPDIR]
- ELSE
- tempfile:=args[ARG_DEVICE]
- ENDIF
-
- AddPart(tempfile,'---PFSDefragTry.tmp---',TEMPFILELEN)
-
- WHILE Fgets(fh_fragfiles,file,MAXLINELEN)
- SetStr(file,StrLen(file))
- IF StrCmp(file,'fragmented file',StrLen('fragmented file'))=TRUE
-
- MidStr(file,file,StrLen('fragmented file '))
- i:=InStr(file,' fragments')
- MidStr(file,file,0,i)
- REPEAT
- MidStr(file,file,0,StrLen(file)-1)
- RightStr(s,file,1)
- UNTIL StrCmp(s,'(')=TRUE
- MidStr(file,file,0,StrLen(file)-1)
- MidStr(file,file,0,StrLen(file)-1)
-
- PrintF('Trying to defragment "\s"... ',file)
- Flush(stdout)
-
- /* check for ctrl */
- IF CtrlC()
- PrintF('\n***Break\n')
- IF fh_fragfiles THEN Close(fh_fragfiles)
- CleanUp(10)
- ENDIF
-
- /* get file information */
- examineok := FALSE
- IF (lock := Lock(file, ACCESS_READ)) > 0
- fib := AllocDosObject(DOS_FIB, NIL)
- IF fib = 0
- Raise('MEM')
- ENDIF
- IF Examine(lock, fib)
- examineok := TRUE
- ELSE
- Raise(ER_EXAMINE)
- ENDIF
- UnLock(lock)
- ELSE
- Raise(ER_NOLOCK)
- ENDIF
-
- /* try to copy file to temporary place */
- i:=copyfile(file,tempfile,buffer,buflen)
- IF i = TRUE
- i:=delfile(file)
- IF i = FALSE
- Raise(ER_DELETE)
- ENDIF
-
- /* copy or rename the file back to original place */
- i:=Rename(tempfile,file)
- IF i=FALSE
- i:=copyfile(tempfile,file,buffer,buflen)
- IF i = TRUE
- delfile(tempfile)
- ELSE
- Raise(ER_COPYING)
- ENDIF
- ENDIF
-
- /* set protection */
- i := SetProtection(file,fib.protection)
- IF i = FALSE
- Raise(ER_PROT)
- ENDIF
-
- /* set file date */
- i := SetFileDate(file,fib.datestamp)
- IF i = FALSE
- Raise(ER_DATE)
- ENDIF
-
- /* set comment */
- i := SetComment(file,fib.comment)
- IF i = FALSE
- Raise(ER_COMMENT)
- ENDIF
- PrintF('ok.\n')
-
- IF fib
- FreeDosObject(DOS_FIB, fib)
- fib := 0
- ENDIF
-
- ELSE
- PrintF('failed!\n')
- delfile(tempfile)
- ENDIF
-
- ENDIF
- ENDWHILE
- ENDIF
- Raise(OK)
-
- EXCEPT
- SELECT exception
- CASE ER_BADARGS; PrintFault(IoErr(), 'PFSDefragTry')
- CASE ER_DISKVALID; PrintF('DiskValid failed!\n')
- PrintFault(IoErr(), 'PFSDefragTry')
- CASE ER_DELETE; PrintF('Could not delete "\s"!\n', file)
- PrintFault(IoErr(), 'PFSDefragTry')
- CASE ER_COPYING; PrintF('Could not copy temporary file "\s"!\n', tempfile)
- PrintFault(IoErr(), 'PFSDefragTry')
- CASE ER_EXAMINE; PrintF('Could not Examine() "\s"!', file)
- PrintFault(IoErr(), 'PFSDefragTry')
- CASE ER_PROT; PrintF('Could not set protection bits for "\s"!', file)
- PrintFault(IoErr(), 'PFSDefragTry')
- CASE ER_DATE; PrintF('Could not set file date for "\s"!', file)
- PrintFault(IoErr(), 'PFSDefragTry')
- CASE ER_COMMENT; PrintF('Could not set comment for "\s"!', file)
- PrintFault(IoErr(), 'PFSDefragTry')
- CASE ER_NOLOCK; PrintF('Could not lock file "\s"!', file)
- PrintFault(IoErr(), 'PFSDefragTry')
- CASE "MEM"; PrintF('Not enough memory!\n')
- DEFAULT; PrintFault(exception,'PFSDefragtry')
- ENDSELECT
-
- IF fib THEN FreeDosObject(DOS_FIB, fib)
- IF buffer THEN FreeVec(buffer)
- IF fh_fragfiles THEN Close(fh_fragfiles)
- IF rdargs THEN FreeArgs(rdargs)
-
- ENDPROC
- CHAR '$VER: PFSDefragTry 1.5 (8.10.1999)',0
-
-